package testview.zhen.com.myapplication.tool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Base64;

import java.io.File;
import java.io.FileInputStream;

@SuppressLint("SimpleDateFormat")
public class AudioRecorder
{

    private MediaRecorder recorder;
    private String fileName;
    // private String fileFolder =
    // Environment.getExternalStorageDirectory().getPath() + "/TestRecord/";
    private String fileFolder = Environment.getExternalStorageDirectory().getPath() + "/";

    private boolean isRecording = false;

    /**
     * 在这里进行录音准备工作，重置录音文件名等
     */
    public void ready()
    {
	File file = new File(fileFolder);
	if (!file.exists())
	{
	    file.mkdir();
	}
	fileName = getCurrentDate();
	recorder = new MediaRecorder();
	recorder.setOutputFile(getFilePath());
	recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置MediaRecorder的音频源为麦克风
//	recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);// 设置MediaRecorder录制的音频格式
//	recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);// 设置MediaRecorder录制音频的编码为amr
//		recorder.setAudioEncodingBitRate(16 * 1024);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
		recorder.setAudioSamplingRate(44100);



    }

    // 以当前时间作为文件名
    private String getCurrentDate()
    {
	return System.currentTimeMillis() + "";// 获取当前时间
    }

    /**
     * 开始录音
     */
    public void start(Context context)
    {
	try
	{
	    if (!isRecording)
	    {
		recorder.prepare();
		recorder.start();
		isRecording = true;
	    }
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
    }

    /**
     * 录音结束
     */
    public void stop()
    {
	try
	{
	    if (isRecording)
	    {
		recorder.stop();
		recorder.release();
		recorder = null;
		isRecording = false;
	    }
	}
	catch (Exception e)
	{
	    recorder = null;
	    e.printStackTrace();
	}
    }

    /**
     * 录音失败时删除原来的旧文件
     */
    public void delete()
    {
	File file = new File(getFilePath());
	file.delete();
    }

    /**
     * 获取录音音量的大小
     * 
     * @return
     */
    public double getAmplitude()
    {
	if (!isRecording)
	{
	    return 0;
	}
	return recorder.getMaxAmplitude();
    }

    /**
     * 返回录音文件完整路径(包含名称)
     * 
     * @return
     */
    public String getFilePath()
    {
	return fileFolder + fileName + ".amr";
    }

    public String getFileName()
    {
	return fileName;
    }

    /**
     * 设置录音文件存放的文件夹
     */
    public void setFileFolder(String fileFolder)
    {
	this.fileFolder = fileFolder;
    }

    public String getBase64()
    {
	try
	{
	    String base64 = Base64.encodeToString(getBytes(), Base64.DEFAULT);
	    return base64;
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
	return null;
    }

    public byte[] getBytes()
    {
	try
	{
	    FileInputStream fis = new FileInputStream(new File(getFilePath()));
	    byte[] bytes = new byte[fis.available()];
	    fis.read(bytes, 0, bytes.length);
	    fis.close();
	    return bytes;
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
	return null;
    }
}